//
//  PhotosListViewModelTest.swift
//  PhotosTests
//
//  Created by wangqi on 2021/10/20.
//

import XCTest
import SWCommonExtensions
@testable import Photos

class PhotosListViewModelTest: XCTestCase {
    
    override func setUpWithError() throws {
        // Put setup code here. This method is called before the invocation of each test method in the class.
    }
    
    override func tearDownWithError() throws {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
    }
    
    func testExample() throws {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.
    }
    
}
extension PhotosListViewModelTest {
    func testGetDisplayDats() -> () {
        let viewModel = getPhotosListViewModel()
        viewModel.datas = getPhotosList(9)
        XCTAssertEqual(viewModel.datas.count, 9)
        let subarray = getDisplayDatas(at: 2, pageSize: viewModel.pageSize, datas: viewModel.datas)
        XCTAssertEqual(subarray.count, 0)
    }
    func testLoadMore() -> () {
        let viewModel = getPhotosListViewModel()
        viewModel.datas = getPhotosList(9)
        let subarray = getDisplayDatas(at: 1, pageSize: viewModel.pageSize, datas: viewModel.datas)
        if subarray.count > 0 {
            viewModel.pageIndex = 1
            viewModel.displayDatas = subarray
        }
        XCTAssertEqual(viewModel.haveNextPage, false)
        viewModel.loadMore {  _, _ in
            XCTAssertEqual(viewModel.pageIndex, 1)
            XCTAssertEqual(viewModel.haveNextPage, false)
        }
    }
    
}

extension PhotosListViewModelTest {
    func getPhotosListViewModel() -> PhotosListViewModel {
        let result = PhotosListViewModel()
        return result
    }
    func getPhotosList(_ count:Int) -> [Photo] {
        let endIndex = count > 0 ? count - 1 : 0
        var result = [Photo]()
        for _ in 0...endIndex {
            let item = Photo()
            result.append(item)
        }
        return result
    }
    func getDisplayDatas(at index: Int, pageSize:Int, datas:[Photo]) -> [Photo] {
        let count = (index - 1) * pageSize
        let fromIndex = index > 1 ? count - 1 : count
        let result = datas.subarray(from: fromIndex, length: pageSize)
        return result
    }
    
}
